package com.home.manager.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.home.manager.bean.User;
import com.home.manager.common.ManagerError;
import com.home.manager.common.Response;
import com.home.manager.common.Role;
import com.home.manager.service.UserService;
import com.home.manager.utils.ResponseBodyBuilder;
import com.home.manager.utils.UserThreadLocal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin")
public class AdminController {


    @Resource
    UserService userService;

    @RequestMapping("/addUser")
    @ResponseBody
    public Response<String> addUser(@RequestBody User user) {
        User currentUser = UserThreadLocal.getCurrentUser();
        String userRole = userService.getUserRole(currentUser.getUserId());
        if (!Role.ADMIN.getName().equals(userRole)) {
            return ResponseBodyBuilder.fail(ManagerError.NOT_ADMIN_ACCESS);
        }
        userService.addUser(user);
        return ResponseBodyBuilder.ok("");
    }

}
